package cn.zktr.Personnel.controllers.ouyangpeng;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.zktr.Personnel.model.service.zhouyongcong.OA.BaseController;
import cn.zktr.Personnel.model.service.zhouyongcong.OA.ResponseResult;
import cn.zktr.Personnel.model.service.zhouyongcong.OA.XlsCSer;
/**
 * 照片上传工具类
 * @author admin
 *
 */
@Controller
public class OARetController extends BaseController{
	@ResponseBody
	@PostMapping("oyp/uploadXls")
	public JSONObject uploadXls(@RequestParam(value="file",required=false)MultipartFile file,HttpServletRequest request,HttpServletResponse response){
		JSONObject jo = null;
		ResponseResult result = new ResponseResult();
		Map<String, Object> map = new HashMap<String, Object>();
        File targetFile=null;
        String url="";//返回存储路径
        int code=1;
        String fileName=file.getOriginalFilename();//获取文件名加后缀
        if(fileName!=null&&fileName!=""){
            String returnUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() +"/static/zyc/xls";//存储路径
            String path = request.getSession().getServletContext().getRealPath("static/zyc/xls"); //文件存储位置
            String fileF = fileName.substring(fileName.lastIndexOf("."), fileName.length());//文件后缀
            
            fileName=new Date().getTime()+"_"+new Random().nextInt(1000)+fileF;//新的文件名
            
            //先判断文件是否存在
            //获取文件夹路径
            File file1 =new File(path);
            
            //如果文件夹不存在则创建    
            if(!file1 .exists()  && !file1 .isDirectory()){       
                file1 .mkdir();  
            }
            //将图片存入文件夹
            targetFile = new File(file1, fileName);
           
            
            try {
            	//将上传的文件写到服务器上指定的文件。
                file.transferTo(targetFile);
                jo = new XlsCSer().Xls(targetFile);
                url=returnUrl+"/"+fileName;
                code=0;
                result.setCode(code);
                result.setMessage("图片上传成功");
                map.put("url", url);
                result.setResult(map);
            } catch (Exception e) {
                e.printStackTrace();
                result.setMessage("系统异常，图片上传失败");
            }
        }
		return jo;
    }
	
	@ResponseBody
	@PostMapping("oyp/uploadImg")
	
    public void uploadPicture(@RequestParam(value="file",required=false)MultipartFile file,HttpServletRequest request,HttpServletResponse response){
        System.err.println("已经进入数据库");
		ResponseResult result = new ResponseResult();
		Map<String, Object> map = new HashMap<String, Object>();
        File targetFile=null;
        String url="";//返回存储路径
        int code=1;
        System.out.println(file);
        String fileName=file.getOriginalFilename();//获取文件名加后缀
        System.out.println(fileName);
        if(fileName!=null&&fileName!=""){   
            String returnUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() +"/static/zyc/images/head-image";//存储路径
            String path = request.getSession().getServletContext().getRealPath("static/zyc/images/head-image"); //文件存储位置
            String fileF = fileName.substring(fileName.lastIndexOf("."), fileName.length());//文件后缀
            
            fileName=new Date().getTime()+"_"+new Random().nextInt(1000)+fileF;//新的文件名
            
            System.out.println("存储路径:"+returnUrl);
            System.out.println("文件存储位置:"+path);
            
            //先判断文件是否存在
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String fileAdd = sdf.format(new Date());
            
            System.out.println("fileAdd:"+fileAdd);
            
            
            //获取文件夹路径
            File file1 =new File(path);
            
            System.out.println("file1:"+file1.getName());
            
            //如果文件夹不存在则创建    
            if(!file1 .exists()  && !file1 .isDirectory()){       
                file1 .mkdir();  
            }
            //将图片存入文件夹
            targetFile = new File(file1, fileName);
           
            
            try {
            	//将上传的文件写到服务器上指定的文件。
                file.transferTo(targetFile);
                url=returnUrl+"/"+fileName;
                code=0;
                result.setCode(code);
                result.setMessage("图片上传成功");
                map.put("url", url);
                result.setResult(map);
            } catch (Exception e) {
                e.printStackTrace();
                result.setMessage("系统异常，图片上传失败");
            }
        }
        writeJson(response, result);
    }
}